Network devices, such as switches, bridges and routers, forward packets through a network based on addresses in the packets. A network device typically includes a plurality of ports coupled to different network links. The network device may receive a packet via one port and process the packet at least to determine via which other port or ports the network switch should transmit the packet. The network device then forwards the packet to the determined one or more other ports. Network devices often store received packets in a packet memory, and process the packets using packet descriptors associated with the packet. After a port for transmission of a packet is determined by the network device, the network device enqueues the packet descriptor associated with the packet in an egress queue corresponding to the port for subsequent scheduling and transmission of the packet via the port. When the packet is scheduled for transmission, the network device dequeues the packet descriptor from the egress queue and retrieves the associated packet from the packet memory. In some cases, the network devices performs modification of the retrieved packet (e.g., modification of a header of the retrieved packet), and then transmits the modified packet from the port of the network device. In some cases, prior to transmission of the packet, the network device performs additional, egress, processing of the packet based on the modified packet. The additional packet processing includes, for example, egress flow classification of the packet, egress policing of the packet, egress Operations, Administration, and Maintenance (OAM) processing of the packet, etc.
Network devices sometimes perform egress packet mirroring to send copies of egressing packets to a processor or an analyzer, for example for performing network monitoring and analytics. In typical network devices, to mirror a packet, the network device generates a copy of the packet descriptor associated with the packet before the packet descriptor is enqueues in an egress queue for subsequent transmission of the packet from the network device. In some situations, however, based on the egress processing of a packet, the packet is dropped or discarded and therefore not actually transmitted from the network device. In such cases, in a typical network device, an egress packet may be mirrored even if the packet is subsequently dropped based on the egress packet processing and therefore not actually transmitted from the network device. Moreover, because egress processing of the packet in a typical network device is performed after the packet is retrieved from the packet memory, the packet cannot be readily copied after egress processing of the packet because the packet is no longer stored in the packet memory.